//
//  SHDetailViewController.m
//  UI_CarHome
//
//  Created by dllo on 15/10/23.
//  Copyright (c) 2015年 沈浩. All rights reserved.
//

#import "SHDetailViewController.h"
#import "SHSaveTool.h"
#import "SHModel.h"
#import "SHDiscountModel.h"
#import "SHHandle.h"
#import "UMSocial.h"

@interface SHDetailViewController ()<UIWebViewDelegate, UMSocialUIDelegate>
@property(nonatomic, retain)UIWebView *detailWebView;
@property(nonatomic, retain)UIActivityIndicatorView *aiView;
@end

@implementation SHDetailViewController

- (void)dealloc
{
    [_detailWebView release];
    [_aiView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blueColor];

    
    [self createNavigationBar];
    [self createWebView];
    [self loadWebData];
    
    
}



- (void)createNavigationBar
{
    [self.navigationItem.leftBarButtonItem setTitle:@"返回"];
    if (self.model) {
        if ([SHSaveTool isHaveCarHomeInPlist:self.model]) {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
        } else {
            self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"已收藏" style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
        }

    } else if (self.webStr) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(share:)];
    }
   
    
}
#pragma mark 点击分享
- (void)share:(UIBarButtonItem *)button {
    NSArray *shareArr = [NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren, nil];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.shareIamge]]];
//    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"563c383667e58e4ed7000fba"
//                                      shareText:self.webStr
//                                     shareImage:image
//                                shareToSnsNames:shareArr
//                                       delegate:self];
//
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"563c383667e58e4ed7000fba" shareText:self.webStr shareImage:image shareToSnsNames:shareArr delegate:nil];
    
   
}

#pragma mark 点击收藏或取消收藏
- (void)click:(UIBarButtonItem *)button
{
    if ([button.title isEqualToString:@"已收藏"]) {
        
        [SHSaveTool cancelCarHomeInPlist:self.model];
        button.title = @"收藏";
#warning 3. 代理人调用协议方法
        [self.delegate takeValue:self.model];
    } else {
        // 收藏进行归档
        [SHSaveTool saveCarHomeToPlist:self.model];
        button.title = @"已收藏";
        
    }
}
#pragma mark 加载数据
- (void)loadWebData
{
    if (self.webStr == nil) {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.model.detailStr]];
        [self.detailWebView loadRequest:request];
    } else {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.webStr]];
        [self.detailWebView loadRequest:request];
    }
}

- (void)createWebView {
    if (!_detailWebView) {
        self.detailWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        [self.view addSubview:self.detailWebView];
        [_detailWebView release]; //重要
        self.detailWebView.delegate = self;
        self.detailWebView.scalesPageToFit = YES;
        self.detailWebView.scrollView.bounces = NO;

    }
}

#pragma mark 风火轮
- (UIActivityIndicatorView *)aiView
{
    if (!_aiView) {
        self.aiView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30 * WID, 30 * HEI)];
        [self.view addSubview:self.aiView];
        [_aiView release];
        self.aiView.center = self.view.center;
        self.aiView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    }
    return _aiView;
}

#pragma mark webView加载开始到加载结束
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.aiView startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.aiView stopAnimating];
    [self.aiView removeFromSuperview];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
